//
//  MapViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import "MBProgressHUD+MJ.h" 

@interface MapViewController ()



- (IBAction)startNavigation:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *startField;
@property (strong, nonatomic) IBOutlet UITextField *endField;

/** 地理编码 */
@property(nonatomic ,strong)CLGeocoder *geocoder;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
   
    

}
-(CLGeocoder *)geocoder
{
    if (_geocoder==nil) {
        _geocoder = [[CLGeocoder alloc]init];
    }
    return _geocoder;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 
 

- (IBAction)startNavigation:(id)sender {
    //1,获取用户输入的起点终点
    NSString * startStr = self.startField.text;
    NSString * endStr = self.endField.text;
    
    if (startStr ==nil ||startStr.length == 0 ||
        endStr == nil || endStr.length == 0) {
        [MBProgressHUD showError:@"请输入地址"];
        return;
    }
    //2,利用GEO对象进行地理编码获取地标对象
    //2.1 获取开始位置的地标
    [self.geocoder geocodeAddressString:startStr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count ==0 ||error != nil) {
            [MBProgressHUD showError:@"请输入地址"];
            return ;
        }
        //开始位置的地标
        CLPlacemark * startPlacemark = [placemarks firstObject];
        
        //3,获得结束位置的地标
        [self.geocoder geocodeAddressString:endStr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (placemarks.count ==0 ||error != nil) {
                [MBProgressHUD showError:@"请输入地址"];
                return ;
            }
            CLPlacemark * endPlacemark = [placemarks firstObject];
            //4,获得地标后开始导航
            [self startNavigationWithStartPlacemark:startPlacemark endPlacemark:endPlacemark];
        }];
        
    }];
 
    
}
/**
 *  利用地标位置开始设置导航
 *
 *  @param startPlacemark 开始位置的地标
 *  @param endPlacemark   结束位置的地标
 */
-(void)startNavigationWithStartPlacemark:(CLPlacemark *)startPlacemark endPlacemark:(CLPlacemark*)endPlacemark
{
    //0,创建起点
    MKPlacemark * startMKPlacemark = [[MKPlacemark alloc]initWithPlacemark:startPlacemark];
    //0,创建终点
    MKPlacemark * endMKPlacemark = [[MKPlacemark alloc]initWithPlacemark:endPlacemark];
    
    //1,设置起点位置
    MKMapItem * startItem = [[MKMapItem alloc]initWithPlacemark:startMKPlacemark];
    //2,设置终点位置
    MKMapItem * endItem = [[MKMapItem alloc]initWithPlacemark:endMKPlacemark];
    //3,起点,终点数组
    NSArray * items = @[startItem ,endItem];
    
    //4,设置地图的附加参数,是个字典
    NSMutableDictionary * dictM = [NSMutableDictionary dictionary];
    //导航模式(驾车,步行)
    dictM[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;
    //地图显示的模式
    dictM[MKLaunchOptionsMapTypeKey] = MKMapTypeStandard;
    
    
    //只要调用MKMapItem的open方法,就可以调用系统自带地图的导航
    //Items:告诉系统地图从哪到哪
    //launchOptions:启动地图APP参数(导航的模式/是否需要先交通状况/地图的模式/..)
    
    [MKMapItem openMapsWithItems:items launchOptions:dictM];
}







@end
